package mortal.component.window
{
	import com.gengine.global.*;
	import com.mui.controls.*;
	import com.mui.display.*;
	import com.mui.manager.*;
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.text.*;
	import mortal.common.global.*;
	import mortal.common.sound.*;
	import mortal.component.gconst.*;
	import mortal.game.manager.*;
	import mortal.mvc.interfaces.*;
	
	public class Window extends GUIComponent implements IView, IDragDrop
	{
		protected var _title:DisplayObject;
		protected var _windowBg:ScaleBitmap;
		protected var _helpBtn:GButton;
		protected var _closeBtn:GButton;
		protected var _titleSprite:Sprite;
		protected var _isAddHelpButton:Boolean = false;
		protected var _isAddClolseButton:Boolean = true;
		protected var _isCenter:Boolean = false;
		protected var _isFirstCenter:Boolean = true;
		protected var contentSprite:Sprite;
		protected var _titleSpriteHight:Number = 30;
		protected var _isFirstShow:Boolean = true;
		private var _stagerect:Rectangle;
		protected var _titleHeight:Number = 48;
		protected var _titleChange:Boolean = false;
		private var _layer:ILayer;
		private var _isHide:Boolean = true;
		
		public function Window()
		{
			this._stagerect = new Rectangle();
			this.layer = LayerManager.windowLayer;
			this.addEventListener(MouseEvent.MOUSE_DOWN, this.onWindowDownHandler);
			
		}
		
		public function get closeBtn() : GButton
		{
			return this._closeBtn;
		}
		
		private function onWindowDownHandler(event:MouseEvent) : void
		{
			PopupManager.setTop(this);	
		}
		
		override protected function createChildren() : void
		{
			super.createChildren();
			this._titleSprite = new Sprite();
			this._titleSprite.addEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDownHandler);
			this.$addChild(this._titleSprite);
			this.contentSprite = new Sprite();
			this.contentSprite.mouseEnabled = false;
			this.$addChild(this.contentSprite);
			if (this._isAddClolseButton)
			{
				this._closeBtn = new GButton();
				this._closeBtn.setSize(19, 19);
				this._closeBtn.label = "";
				this._closeBtn.styleName = "CloseButton";
				this._closeBtn.addEventListener(MouseEvent.CLICK, this.closeBtnClickHandler);
				this.$addChild(this._closeBtn);
			}
			if (this._isAddHelpButton)
			{
				this._helpBtn = new GButton();
				this._helpBtn.setSize(20, 19);
				this._helpBtn.label = "";
				this._helpBtn.styleName = "HelpButton";
				this._helpBtn.addEventListener(MouseEvent.CLICK, this.onHelpBtnClickHandler);
				this.$addChild(this._helpBtn);
			}
			
		}
		
		override public function addChild(param1:DisplayObject) : DisplayObject
		{
			return this.contentSprite.addChild(param1);
		}
		
		override public function removeChild(param1:DisplayObject) : DisplayObject
		{
			return this.contentSprite.removeChild(param1);
		}
		
		override public function addChildAt(param1:DisplayObject, param2:int) : DisplayObject
		{
			return this.contentSprite.addChildAt(param1, param2);
		}
		
		override public function removeChildAt(param1:int) : DisplayObject
		{
			return this.contentSprite.removeChildAt(param1);
		}
		
		override public function contains(param1:DisplayObject) : Boolean
		{
			return this.contentSprite.contains(param1);
		}
		
		protected function $addChild(param1:DisplayObject) : DisplayObject
		{
			return super.addChild(param1);
		}
		
		protected function $removeChild(param1:DisplayObject) : DisplayObject
		{
			return super.removeChild(param1);
		}
		
		protected function $addChildAt(param1:DisplayObject, param2:int) : DisplayObject
		{
			return super.addChildAt(param1, param2);
		}
		
		protected function $removeChildAt(param1:int) : DisplayObject
		{
			return super.removeChildAt(param1);
		}
		
		protected function onMouseDownHandler(event:MouseEvent) : void
		{
			Global.stage.addEventListener(MouseEvent.MOUSE_UP, this.onMouseUpHandler);
			this._stagerect.x = 0;
			this._stagerect.y = 0;
			this._stagerect.width = this.stage.stageWidth;
			this._stagerect.height = this.stage.stageHeight - this._titleHeight;
			this.startDrag(false, this._stagerect);
			
		}
		
		private function onMouseUpHandler(event:MouseEvent) : void
		{
			Global.stage.removeEventListener(MouseEvent.MOUSE_UP, this.onMouseUpHandler);
			this.stopDrag();
			
		}
		
		private function setTitleSprite(param1:Number, param2:Number) : void
		{
			this._titleSprite.graphics.clear();
			this._titleSprite.graphics.beginFill(0, 0);
			this._titleSprite.graphics.drawRect(0, 0, param1, param2);
			this._titleSprite.graphics.endFill();
			
		}
		
		protected function closeBtnClickHandler(event:MouseEvent) : void
		{
			Global.stage.focus = Global.stage;
			SoundManager.instance.soundPlay(SoundTypeConst.UIclose);
			this.hide();
			
		}
		
		protected function onHelpBtnClickHandler(event:MouseEvent) : void
		{
			
		}
		
		public function set titleHeight(param1:Number) : void
		{
			this._titleHeight = param1;
			
		}
		
		public function get titleHeight() : Number
		{
			return this._titleHeight;
		}
		
		public function get title() : *
		{
			return this._title;
		}
		
		public function set title(param1:*) : void
		{
			var _loc_2:TextField = null;
			if (this._title && this._title.parent)
			{
				this._title.parent.removeChild(this._title);
				this._title = null;
			}
			if (param1 is DisplayObject)
			{
				this._title = param1;
				this._titleSprite.addChild(this._title);
			}
			else if (param1 is String)
			{
				_loc_2 = new TextField();
				_loc_2.mouseEnabled = false;
				_loc_2.embedFonts = true;
				_loc_2.defaultTextFormat = GlobalStyle.windowTitle;
				_loc_2.selectable = false;
				_loc_2.filters = [FilterConst.titleFilter, FilterConst.titleShadowFilter];
				_loc_2.autoSize = TextFieldAutoSize.LEFT;
				_loc_2.htmlText = param1;
				this._title = _loc_2;
				this._titleSprite.addChild(this._title);
			}
			this._titleChange = true;
			this.updateDisplayList();
			
		}
		
		override protected function updateSize() : void
		{
			super.updateSize();
			this.updateBtnSize();
			this.setTitleSprite(this.width, this._titleSpriteHight);
			
		}
		
		protected function updateBtnSize() : void
		{
			if (this._closeBtn)
			{
				this._closeBtn.x = this.width - this._closeBtn.width - 8;
				this._closeBtn.y = 7;
			}
			if (this._helpBtn)
			{
				this._helpBtn.x = this._closeBtn.x - 26;
				this._helpBtn.y = this._closeBtn.y;
			}
			
		}
		
		override protected function updateDisplayList() : void
		{
			super.updateDisplayList();
			if (this._titleChange)
			{
				this._titleChange = false;
				if (this._title)
				{
					this._title.y = 4;
					this._title.x = (this.width - this._title.width) / 2;
				}
			}
			
		}
		
		public function dispose() : void
		{
			this._closeBtn.removeEventListener(MouseEvent.CLICK, this.closeBtnClickHandler);
			this._closeBtn = null;
			
		}
		
		public function set layer(param1:ILayer) : void
		{
			this._layer = param1;
			
		}
		
		public function get layer() : ILayer
		{
			return this._layer;
		}
		
		public function get isHide() : Boolean
		{
			return this._isHide;
		}
		
		public function update(param1:Object, ... args) : void
		{
			
		}
		
		public function hide() : void
		{
			if (this._layer && !this._isHide)
			{
				this.stopDrag();
				Global.stage.removeEventListener(MouseEvent.MOUSE_UP, this.onMouseUpHandler);
				this._isHide = true;
				this._layer.removePopup(this);
				this.dispatchEvent(new WindowEvent(WindowEvent.CLOSE));
			}
			
		}
		
		public function show(param1:int = 0, param2:int = 0) : void
		{
			if (this._layer)
			{
				if (this._isHide)
				{
					this._isHide = false;
					this._layer.addPopUp(this);
					if (this._isFirstCenter)
					{
						this._layer.centerPopup(this);
						this._isFirstCenter = this._isCenter;
					}
					this.dispatchEvent(new WindowEvent(WindowEvent.SHOW));
				}
				else
				{
					this._layer.setTop(this);
				}
				if (param1 != 0 && param2 != 0)
				{
					this._layer.setPosition(this, param1, param2);
				}
			}
			
		}
		
		public function leftToChat() : void
		{
			if (this.layer is WindowLayer)
			{
				(this.layer as WindowLayer).leftToChat(this);
			}
			
		}
		
		public function rightToTask() : void
		{
			if (this.layer is WindowLayer)
			{
				(this.layer as WindowLayer).rightToTask(this);
			}
			
		}
		
		public function get isDragAble() : Boolean
		{
			return false;
		}
		
		public function get isDropAble() : Boolean
		{
			return true;
		}
		
		public function get isThrowAble() : Boolean
		{
			return false;
		}
		
		public function get dragSource() : Object
		{
			return {};
		}
		
		public function set dragSource(param1:Object) : void
		{
			
		}
		
		public function canDrop(param1:IDragDrop, param2:IDragDrop) : Boolean
		{
			return true;
		}
		
		public function get isAddHelpButton() : Boolean
		{
			return this._isAddHelpButton;
		}
		
		public function set isAddHelpButton(param1:Boolean) : void
		{
			this._isAddHelpButton = param1;
			
		}
		
	}
}
